package com.longrise.android.byjk.plugins.tabfirst.revision;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.plugins.tabfirst.answerQuestion.CommitQuestionActivity;
import com.longrise.android.byjk.plugins.tabfirst.revision.adapter.HomeFragAdapter;
import com.longrise.android.byjk.utils.Tracker_Google;

/**
 * Created by sujizhong on 2017/2/21.
 * 问答
 */
public class QuestionsActivity extends BaseActivity {

    private static final String TAG = "QuestionsActivity";

    private final String SAVE_KEY_CURRENT_PAGE = "SAVE_KEY_CURRENT_PAGE";

    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private HomeFragAdapter mAdapter;
    private TextView mBtn;

    private RelativeLayout mBack;
    private TextView mTitle;

    private int mCurrentPage = 0;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_questions;
    }

    @Override
    public void initView() {
        mViewPager = (ViewPager) findViewById(R.id.container_cons);
        mTabLayout = (TabLayout) findViewById(R.id.tablayout_cons);
        mBack = (RelativeLayout) findViewById(R.id.back_new);
        mTitle = (TextView) findViewById(R.id.tv_title_new);
        mBtn = (TextView) findViewById(R.id.bt_question);
        adapter();

        setTitle();
        onEvent();

        Tracker_Google.getInstance().addGoogleScreenName("保宝问答");
    }

    private void setTitle() {
        if (mTitle != null) {
            mTitle.setText(getString(R.string.wd));
        }
    }

    private void onEvent() {
        if (mBack != null) {
            mBack.setOnClickListener(mOnClickListener);
        }
        if (mBtn != null) {
            mBtn.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    int action = event.getAction();
                    switch (action) {
                        case MotionEvent.ACTION_DOWN:
                        case MotionEvent.ACTION_BUTTON_PRESS:
                            ScaleAnimation animation = new ScaleAnimation(1f, 0.8f, 1f, 0.8f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                            animation.setDuration(200);
                            animation.setFillAfter(true);
                            mBtn.startAnimation(animation);
                            break;

                        case MotionEvent.ACTION_UP:
                        case MotionEvent.ACTION_CANCEL:
                            ScaleAnimation animation1 = new ScaleAnimation(0.8f, 1f, 0.8f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                            animation1.setDuration(200);
                            animation1.setFillAfter(true);
                            mBtn.startAnimation(animation1);
                            animation1.setAnimationListener(new Animation.AnimationListener() {
                                @Override
                                public void onAnimationStart(Animation animation) {
                                }

                                @Override
                                public void onAnimationEnd(Animation animation) {
                                    openCommitQuestionActivity();
                                }

                                @Override
                                public void onAnimationRepeat(Animation animation) {
                                }
                            });
                            break;
                    }
                    return true;
                }
            });
        }
    }

    private void openCommitQuestionActivity() {
        Intent intent = new Intent(this, CommitQuestionActivity.class);
        startActivityForResult(intent, 0);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (resultCode == 100 && data != null) {
                boolean isRefush = data.getBooleanExtra(CommitQuestionActivity.RESULT_BOOLEAN_STATUS, false);
                if (isRefush && mViewPager != null && mAdapter != null) {
                    if (mViewPager.getCurrentItem() != 0) {
                        mViewPager.setCurrentItem(0);
                    }
                    mAdapter.mNewComFragment.onRefresh();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.back_new) {
                close();
            }
        }
    };

    private void adapter() {
        mAdapter = new HomeFragAdapter(getSupportFragmentManager(), HomeFragAdapter.mConFrag);
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout) {
            @Override
            public void onPageSelected(int position) {
                addEvent(position);
            }
        });
        mTabLayout.setupWithViewPager(mViewPager);
    }

    private void release() {
        mViewPager = null;
        mTabLayout = null;
        mAdapter = null;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mViewPager != null) {
            outState.putInt(SAVE_KEY_CURRENT_PAGE, mViewPager.getCurrentItem());
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null && mViewPager != null) {
            mCurrentPage = savedInstanceState.getInt(SAVE_KEY_CURRENT_PAGE);
            mViewPager.setCurrentItem(mCurrentPage);
        }
    }

    private void close() {
        finish();
    }

    private void addEvent(int position) {
        if (position == 0) {
            Tracker_Google.getInstance().addEvent("QA", "Latest-questions", "最新提问");
        } else {
            Tracker_Google.getInstance().addEvent("QA", "Recommended-answer", "推荐回答");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        release();
    }
}
